package assignment3;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;

public class OneWayHash
{
    private static String _hash(String in)
    {
        String passwordToHash = in; //salt(in);
        String generatedPassword = null;
        try {
            // Create MessageDigest instance for MD5
            MessageDigest md = MessageDigest.getInstance("MD5");
            //Add password bytes to digest
            md.update(passwordToHash.getBytes());
            //Get the hash's bytes
            byte[] bytes = md.digest();
            //This bytes[] has bytes in decimal format;
            //Convert it to hexadecimal format
            StringBuilder sb = new StringBuilder();
            for(int i=0; i< bytes.length ;i++)
            {
                sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
            }
            //Get complete hashed password in hex format
            generatedPassword = sb.toString();
        }
        catch (NoSuchAlgorithmException e)
        {
            e.printStackTrace();
        }
        return generatedPassword;
    }
    
    public static String salt(String in)
    {
        return _hash("00$#@$%&^@0111" + in + "$#@$%&^@");
    }
    
    public static String hash(String in)
    {
        return _hash(salt(in));
    }
    
    
//    public static void main(String[] args)
//    {
//        while(true){
//            Scanner scanner = new Scanner(System.in);
//            System.out.println("Enter password:");
//            String password = scanner.nextLine();
//            System.out.println("hashed password:" + hash(password) + ":" + password);
//        }
//        
//    }
}